package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Library {

    Librarian librarian=new Librarian();


    public static String showWelcomeMsg()
    {
        String welcomeMsg="Welcome to the Biblioteca Library Management App!";
        return welcomeMsg;
    }



    public String displayBookLibraryMenu()
    {
        System.out.println();
        String libraryFunctions="\nEnter the serial number of your choice\n1. Add books to Library\n2. List Books\n3. Return Book\n4. CheckOut Book\n5. View Borrower List\n6. Go to main menu";
        return libraryFunctions;
    }


    public String displayMovieLibraryMenu()
    {
        System.out.println();
        String libraryFunctions="\nEnter the serial number of your choice\n1. Add movies to Library\n2. List Movies\n3. Return Movie\n4. CheckOut Movie\n5. View Borrower List\n6. Go to main menu";
        return libraryFunctions;
    }


    public int acceptLibraryFunction()
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int choiceNumber=0;
        try {
            choiceNumber=Integer.parseInt(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return choiceNumber;
    }



    public String forwardControlToBookLibrary(int choiceNumber)
    {

            switch(choiceNumber)
            {
                case 1:librarian.addBook();
                    break;
                case 2: librarian.listAvailableBooks();
                    break;
                case 3: librarian.returnBook();
                    break;
                case 4: librarian.checkOutBook();
                    break;
                case 5: librarian.viewListOfBookBorrowers();
                    break;
                case 6: return "exit";

                default: System.out.println("Wrong choice... Please select a valid option!");
                    break;
            }
            return null;

    }

    public String forwardControlToMovieLibrary(int choiceNumber)
    {

        switch(choiceNumber)
        {
            case 1:librarian.addMovie();
                break;
            case 2: librarian.listAvailableMovies();
                break;
            case 3: librarian.returnMovie();
                break;
            case 4: librarian.checkOutMovie();
                break;
            case 5: librarian.viewListOfMovieBorrowers();
                break;
            case 6: return "exit";

            default: System.out.println("Wrong choice... Please select a valid option!");
                break;
        }
        return null;
    }

    public String acceptUserLoginCredentials()
    {
        System.out.println("Please enter your library number and password separated by :");
        String userCredentials=null;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        try {
             userCredentials=br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return userCredentials;
    }




    public String acceptChoiceOfLibraryToEnter() {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        String libraryChoice=null;
        try {
            libraryChoice=br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return libraryChoice;
    }



    public void getUserAccountFromLibrarian(String userCredentials)
    {

        UserAccount userAccount;
        userAccount=librarian.searchForUserAccount(userCredentials);
        showMainMenuToUser(userAccount);

    }




    private void showMainMenuToUser(Object userAccount)
    {
        if(userAccount!=null)
        {

            while(true)
            {
                System.out.println("What would you like to do?\n1. Visit Book Library\n2. Visit Movie Library\n3. Exit Biblioteca App\n Please enter your choice");
                String choiceOfLibrary=acceptChoiceOfLibraryToEnter();
                String returnedValue=null;
                if(choiceOfLibrary.equals("1"))
                {
                    while(returnedValue!="exit")
                    {
                        System.out.println(displayBookLibraryMenu());
                        returnedValue=forwardControlToBookLibrary(acceptLibraryFunction());
                    }
                }
                else if(choiceOfLibrary.equals("2"))
                {
                    while(returnedValue!="exit")
                    {
                        System.out.println(displayMovieLibraryMenu());
                        returnedValue=forwardControlToMovieLibrary(acceptLibraryFunction());
                    }

                }
                else if(choiceOfLibrary.equals("3"))
                    break;
                else
                    System.out.println("Invalid choice! Please enter again.");

            }

        }

        else
        {
            System.out.println("Sorry! Invalid User Credentials. Exiting Biblioteca App.");
        }

    }
}
